package com.zretc.notice.controller;


import cn.hutool.core.util.StrUtil;
import com.zretc.common.entity.Result;
import com.zretc.notice.pojo.Activity;
import com.zretc.notice.service.ActParticipantsService;
import com.zretc.notice.service.ActivityService;
import com.zretc.security.entity.LoginUser;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;

/**
 * 功能：
 * 作者：111
 * 日期：2024/7/4 23:07
 */
@RestController
public class ActivityController {

    @Resource
    ActivityService activityService;

    @Resource
    ActParticipantsService actParticipantsService;


    @GetMapping("/notice/activity/showactivitydetail")
    public Result showActivityDetail(@RequestParam Integer activityId){
        return Result.success(activityService.showActivityDetail(activityId));
    }
    @PostMapping("/notice/activity/add")
    public Result addActivity(@RequestBody Activity activity){

        if(StrUtil.isBlank(activity.getActivityContent())||StrUtil.isBlank(activity.getActivityTopic())||StrUtil.isBlank(activity.getEndTime().toString())){
            return Result.error("数据不合法");
        }
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        LoginUser user = (LoginUser) authentication.getPrincipal();
        activity.setPromulgatorId(user.getUser().getUserId());
        activityService.addActivity(activity);
        return Result.success(activity);
    }

    @GetMapping("/notice/activity/show")
    public Result showActivity(){
        return Result.success(activityService.showActivity());
    }


//    删除活动
    @PutMapping("/notice/activity/delete/{id}")
    public Result deleteActivity(@PathVariable("id") Integer activityId){
        activityService.deleteActivity(activityService.showActivityDetail(activityId));
        return Result.success(activityService.showActivityDetail(activityId));
    }
    @GetMapping("/notice/activity/record")//当前用户
    public Result showRecordActivity(){

        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        LoginUser user = (LoginUser) authentication.getPrincipal();
        return Result.success(activityService.showTrackRecord(actParticipantsService.selectActivity(user.getUser().getUserId())));
    }


}